package com.beijiteshop.shop.ui.notification.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.beijiteshop.libcommon.base.adapter.showEmpty
import com.beijiteshop.libcommon.base.adapter.showError
import com.beijiteshop.libcommon.base.adapter.showLoading
import com.beijiteshop.libcommon.base.adapter.showNetError
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.shop.R
import com.beijiteshop.shop.app.PageIndex
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentNotificationDetailBinding
import com.beijiteshop.shop.model.api.response.NotificationDetailEntity
import com.beijiteshop.shop.ui.coupon.fragment.CouponFragment
import com.beijiteshop.shop.ui.notification.adapter.NotificationDetailAdapter
import com.beijiteshop.shop.ui.notification.vm.NotificationVM
import com.beijiteshop.shop.ui.order.fragment.OrderDetailFragment
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/16 11:06
 * @Description 通知详情页面
 */
class NotifyDetailFragment : BaseContainerFragment(R.layout.fragment_notification_detail) {

    companion object{
        private const val INIT_PAGE = 1
    }

    private val mBinding by bindView<FragmentNotificationDetailBinding>()
    private val mNotifyDetailVM: NotificationVM by viewModel()
    private val mNotificationAdapter = NotificationDetailAdapter(mutableListOf())
    private var mPage = INIT_PAGE

    override fun initView(view: View) {
        initRV()
    }

    override fun initData() {
    }

    override fun initEvent() {
        startObserve()

        mBinding.notificationTitleBarBackIv.setOnClickListener {
            close()
        }

        mBinding.notificationTitleBarBtnIv.setOnClickListener {
            setAllRead()
        }
    }

    override fun onLazyLoad() {
        initList()
    }

    private fun initRV(){
        mNotificationAdapter.apply {
            loadMoreModule.setOnLoadMoreListener {
                loadMoreData()
            }

            setOnItemClickListener { _, _, position ->
                if (data[position].itemType == NotificationDetailEntity.TYPE_ITEM){
                    val item = data[position].message
                    item?.let {message ->
                        if (message.bus_OrdersID.isNullOrEmpty()){//优惠券
                            val bundle = Bundle().apply {
                                putInt(CouponFragment.BUNDLE_TYPE,message.status.toInt())
                            }
                            startActivity(PageIndex.COUPON,bundle)
                        }else{//订单
                            val bundle = Bundle().apply {
                                putString(OrderDetailFragment.BUNDLE_ORDER_ID,message.bus_OrdersID)
                            }
                            startActivity(PageIndex.ORDER_DETAIL,bundle)
                        }
                        if (!item.isRead){
                            setReadState(message.isOrder,message.bus_MessageID)
                        }
                    }
                }
            }
        }

        mBinding.notificationDetailRv.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mNotificationAdapter
        }
    }

    private fun startObserve(){

        mNotifyDetailVM.apply {
            liveDetails.observeKt {
                if (mPage == INIT_PAGE){
                    mNotificationAdapter.setNewInstance(it)
                }else{
                    updateRVState(false,it)
                }
            }

            liveEmpty.observeKt {
                if (mPage == INIT_PAGE){
                    mNotificationAdapter.showEmpty(it){
                        initList()
                    }
                }else{
                    updateRVState(false,null)
                }
            }

            liveError.observeKt {
                if (mPage == INIT_PAGE){
                    mNotificationAdapter.showError(it){
                        initList()
                    }
                }else{
                    updateRVState(true,null)
                }
            }

            liveNetError.observeKt {
                if (mPage == INIT_PAGE){
                    mNotificationAdapter.showNetError(it){
                        initList()
                    }
                }else{
                    updateRVState(true,null)
                }
            }

            liveReadState.observeKt {messageId ->
                messageId?.let {
                    mNotificationAdapter.updateReadState(it)
                }
            }

            liveAllRead.observeKt {message ->
                message?.let {
                    showToastInCenter(it)
                    mNotificationAdapter.updateAllReadState()
                }
            }
        }
    }

    private fun initList(){
        mNotificationAdapter.showLoading()
        mNotifyDetailVM.getNotificationList(mPage)
    }

    private fun loadMoreData(){
        mPage++
        mNotifyDetailVM.getNotificationList(mPage)
    }

    /**
     * 更新列表加载状态
     */
    private fun updateRVState(error: Boolean,data: MutableList<NotificationDetailEntity>?){
        if (error){
            if (mNotificationAdapter.loadMoreModule.isLoading){
                mNotificationAdapter.loadMoreModule.loadMoreFail()
            }
        }else{
            if (mNotificationAdapter.loadMoreModule.isLoading){
                if (data.isNullOrEmpty()){
                    mNotificationAdapter.loadMoreModule.loadMoreEnd()
                }else{
                    mNotificationAdapter.addData(data)
                    mNotificationAdapter.loadMoreModule.loadMoreComplete()
                }
            }
        }
    }

    /**
     * 设置已读状态
     */
    private fun setReadState(isOrder: Boolean,messageId: String){
        mNotifyDetailVM.setReadState(isOrder,messageId)
    }

    /**
     * 设置全部已读
     */
    private fun setAllRead(){
        if (!mNotificationAdapter.data.isNullOrEmpty()){
            mNotifyDetailVM.setAllRead()
        }
    }
}